// 权限模块
import router from '@/router'
import useUserStore from './stores/user'
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

// 设置白名单，白名单可以让没有登录的用户访问
const whiteList = ['/login']

//设置路由前置守卫
// 从from组件跳转到to组件 跳转之前
router.beforeEach((to, from, next) => {
  // start()开启滚动条
  NProgress.start()
  const user = useUserStore()
  const token = user.userinfo.token

  if (token) {
    // 登录了
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 未登录
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

// 路由后置守卫  没有next
router.afterEach((to, from) => {
  NProgress.done()
})
